﻿using System;
using System.Web.Mvc;

namespace dPet.Web.Admin.Controllers
{
	using dPet.Infrastructure.Logging;
	using dPet.Web.Admin.Fliters;
	using dPet.Web.Admin.ViewModels;
	using Hangerd.Mvc;
	using Hangerd.Mvc.Authentication;
	using Hangerd.Mvc.ViewModels;

	public class HomeController : HangerdController
	{
		private readonly ILoginLoggingService _loginLoggingService;

		public HomeController(ILoginLoggingService loginLoggingService)
		{
			_loginLoggingService = loginLoggingService;
		}

		[AdminLoginAuth]
		public ActionResult Index()
		{
			return View();
		}

		#region Authentication

		public ActionResult Login()
		{
			return View();
		}

		[HttpPost]
		public ActionResult Login(LoginModel model)
		{
			if (AdministratorModel.Validate(model.LoginName, model.Password))
			{
				LoginHelper.Login(model.LoginName, string.Empty, DateTime.Now.AddHours(2));

				//登录日志
				_loginLoggingService.AddLog(model.LoginName, base.Request);

				return RedirectToAction("Index");
			}

			return View();
		}

		public ActionResult SignOut()
		{
			LoginHelper.SignOut();

			return RedirectToAction("Login");
		}

		#endregion
	}
}
